package de.wbh.gwt.form.layouter.java.server.servlet;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;

import de.wbh.gwt.form.layouter.java.server.ConfigurationHelper;

public class ImageServlet extends HttpServlet {

	private static String dir = ConfigurationHelper.getInstance().getHomeDirectory();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("image/png");

		String fileName = Iterables.getLast(Splitter.on("/").split(req.getRequestURI()));
		File f = new File(dir + "\\image\\" + fileName + ".png");
		BufferedImage bi = ImageIO.read(f);
		OutputStream out = response.getOutputStream();
		ImageIO.write(bi, "png", out);
		out.close();

	}

}
